package com.cw.jeeyt.controller.api;

import java.util.ArrayList;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.api.ApiTableAbbreviationBo;
import com.cw.jeeyt.service.intf.api.ApiTableAbbreviationService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.GsonUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 数据库表缩写配置控制层
 *
 * @author chenw
 * @date 2019-04-17 13:17:14
 */
@Controller
@RequestMapping("/apiTableAbbreviation")
public class ApiTableAbbreviationController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ApiTableAbbreviationController.class);
    @Resource
    private ApiTableAbbreviationService apiTableAbbreviationService;//数据库表缩写配置业务接口

    /**
     * 跳转到数据库表缩写配置列表查询页面
     *
     * @param databaseId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query(Long databaseId) throws Exception {

        ModelAndView mv = new ModelAndView("api/apiTableAbbreviation_index");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("databaseId", databaseId);
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable datatable数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listApiTableAbbreviation")
    public ResultData listApiTableAbbreviation(@RequestBody DataTable dataTable, Long databaseId) throws Exception {

        ApiTableAbbreviationBo apiTableAbbreviationBo = GsonUtil.json2Obj(
            GsonUtil.objectToJsonString(dataTable.getQueryCond()), ApiTableAbbreviationBo.class);
        if (apiTableAbbreviationBo == null) {
            apiTableAbbreviationBo = new ApiTableAbbreviationBo();
        }
        apiTableAbbreviationBo.setDatabaseId(databaseId);
        PageInfo<ApiTableAbbreviationBo> pageInfo = apiTableAbbreviationService.queryByPage(apiTableAbbreviationBo,
            dataTable.getStart(), dataTable.getLength(), dataTable.getOrder()[0]);
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到数据库表缩写配置表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editEdit(Long id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("api/apiTableAbbreviation_form");
        ApiTableAbbreviationBo apiTableAbbreviationBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            apiTableAbbreviationBo = new ApiTableAbbreviationBo();
        } else {
            apiTableAbbreviationBo = apiTableAbbreviationService.getApiTableAbbreviationById(id);
        }
        mv.addObject("apiTableAbbreviation", apiTableAbbreviationBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 根据操作类型保存数据库表缩写配置数据
     *
     * @param response
     * @param apiTableAbbreviationBo
     * @param operation add 新增  edit 修改
     */
    @ResponseBody
    @RequestMapping(value = "/saveApiTableAbbreviation", method = RequestMethod.POST)
    public JsonResultDto saveApiTableAbbreviation(HttpServletResponse response,
        ApiTableAbbreviationBo apiTableAbbreviationBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                apiTableAbbreviationService.insert(apiTableAbbreviationBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                apiTableAbbreviationService.update(apiTableAbbreviationBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("保存失败:" + e.getMessage());
            e.printStackTrace();
            logger.error("保存数据库表缩写配置失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 删除数据库表缩写配置
     *
     * @param apiTableAbbreviationBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(HttpServletResponse response,
        ApiTableAbbreviationBo apiTableAbbreviationBo) throws Exception {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            apiTableAbbreviationService.delete(apiTableAbbreviationBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("删除失败：" + e.getMessage());
            logger.error("删除数据库表缩写配置失败：" + e.getMessage());
        }
       return jsonResultDto;
    }
}
